var jice = require('./library/jice.weapp.js')

App({
  onLaunch: function() {
    jice.init(this.G.Appid, this.G.JcAppid)
    try {
      var uid = wx.getStorageSync('uid')
      var token = wx.getStorageSync('token')
      var openid = wx.getStorageSync('openid')
      var userInfo = wx.getStorageSync('userInfo')
      if(!uid || !openid || !userInfo || !token) {
        this.userLogin()
      } else {
        wx.request({
          url: this.G.Server + 'v2/sign/check/',
          data: {
            uid: uid
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: (retCheck) => {
            if(!retCheck.data.exists) {
              wx.clearStorageSync()
              this.userLogin()
            } else {
              this.G.user.uid = uid
              this.G.user.token = token
            }
          }
        })
        this.JiCe(openid, userInfo)
      }
    } catch(e) {
      console.log('获取本地缓存的uid失败')
    }
  },
  onShow: function() {
    jice.track("appshow")
  },
  userLogin: function() {
    wx.login({
      success: (retLogin) => {
        var sid = false;//wx.getStorageSync('sid')
        if(!sid) {
          wx.request({
            url: this.G.Server + 'v2/sign/session/',
            data: {
              code: retLogin.code,
              appid: this.G.Appid
            },
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: (retSession) => {
              if(retSession.data.s) {
                //wx.setStorageSync('sid', retSession.data.sid)
                this.getUserInfo(retSession.data.sid)
              }
            }
          })
        } else {
          this.getUserInfo(sid)
        }
      }
    })
  },
  getUserInfo: function(sid) {
    wx.getUserInfo({
      success: (res) => {
        wx.setStorageSync('userInfo', res.rawData)
        wx.request({
          url: this.G.Server + 'v2/sign/user/',
          data: {
            data: res.encryptedData,
            iv: res.iv,
            sid: sid,
            userInfo: res.rawData,
            appid: this.G.Appid
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: (r) => {
            r.statusCode == 200 ? r = r.data : () => {
              this.userLogin()
            }
            if(r.code == 200) {
              wx.setStorageSync('uid', r.uid)
              wx.setStorageSync('token', r.token)
              wx.setStorageSync('openid', r.openid)
              wx.removeStorage({
                key: 'sid',
                success: function(res) {
                }
              })
              this.G.user.uid = r.uid
              this.G.user.token = r.token
              this.JiCe(r.openid, res.rawData)
            } else {
              wx.clearStorageSync()
              //this.G.myAlert('登录失败，请关闭小程序重试~错误码[' + r.code + ']')
              this.userLogin()
            }
          }
        })
      },
      fail: (ret) => {
        this.G.myAlert('您拒绝了用户信息授权，您将不能享受完整的功能~')
      }
    })
  },
  JiCe: function(openid, user) {
    jice.addUserIdentifier(openid, {
      nickname: user.nickName,
      wxgender: user.gender,
      wxcountry: user.country,
      wxprovince: user.province,
      wxcity: user.city,
      avatarurl: user.avatarUrl
    })
    jice.track("applaunch")
  },
  jice: jice,
  G: {
    channel: 1,
    view: 0,
    click: 1,
    user: {
      uid: null,
      token: null
    },
    //Appid: 'wx8586cc7e500912d6',
    //Wss: 'ws://guiwh.server.joypaw.cn:9580',
    //Server: 'https://epclass.guiwh.server.joypaw.cn/weapp/',
    Appid: 'wxe483592aaac31690',
    MchId: '1230362602',
    //Wss: 'wss://tepclass.meadjohnson.joypaw.com/wss/',
    //Server: 'https://tepclass.meadjohnson.joypaw.com/weapp/',
    Wss: 'wss://epclass.meadjohnson.joypaw.com/wss/',
    Server: 'https://epclass.meadjohnson.joypaw.com/weapp/',
    JcAppid: '18593e658afb4259912d38dc68ff3062',
    myAlert: function(msg) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: msg,
        success: function(res) {
        }
      })
    }
  },
  logger: function(data) {
    var post = data
    post.uid = this.uid()
    wx.request({
      url: this.G.Server + 'v2/user/logger/',
      data: post,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: (r) => {
      }
    })
  },
  uid: function() {
    if(this.G.user.uid) {
      return this.G.user.uid
    }
    var uid = wx.getStorageSync('uid')
    if(uid) {
      return uid
    }
    return false
  },
  token: function() {
    if(this.G.user.token) {
      return this.G.user.token
    }
    var token = wx.getStorageSync('token')
    if(token) {
      return token
    }
    return false
  }
})
